package com.dangdang.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.extra.mail.MailUtil;
import com.dangdang.entity.User;
import com.dangdang.service.UserService;
import com.dangdang.service.impl.UserServiceImpl;
import com.dangdang.util.JedisUtils;
import com.dangdang.util.MyEmailUtil;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("user")
public class UserController {
    private UserService us = new UserServiceImpl();
    @RequestMapping("login")
    public String login(String email,String password,HttpSession session){
        User user = us.login(email, password);
        if(user!=null&&user.getState()==1){
            session.setAttribute("user",user);
            String sessionId = session.getId();
            Jedis jedis = JedisUtils.getJedis();
            return "redirect:/book/selectByChoice.do";
        }else{
            return "redirect:/user/login_form.jsp?error=1";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect://book/selectByChoice.do";
    }
    @RequestMapping("/createAuthCode")
    public void createAuthCode(HttpServletResponse response, HttpSession session){
        ServletOutputStream outputStream = null;
        try {
            //生成验证码图片
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 50);
            //把验证码图片交给response响应给浏览器
            //1.获取response的响应流
            outputStream = response.getOutputStream();
            //2.把验证码图片放入response的响应流中
            lineCaptcha.write(outputStream);
            //获取生成的验证码的值
            String code = lineCaptcha.getCode();
            //将code放入session作用域
            session.setAttribute("code",code);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            //关闭流
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
    @RequestMapping("/regist")
    public String regist(User user, String number, HttpSession session, Model model) {
        String code = (String) session.getAttribute("code");
        if (number.equals(code)) {
            user.setState(0);
            Boolean regist = us.regist(user);
            if(!regist){
                return "forward:/user/register_form.jsp";
            }
            MyEmailUtil.sendEmail(user.getEmail());
            return "forward:/user/verify_form.jsp";
        } else {
            model.addAttribute("inputuser",user);
            return "forward:/user/register_form.jsp?error=1";
        }
    }
    @RequestMapping("/changeState")
    public String changeState(String email,HttpSession session){
        User user = us.selectByEmail(email);
        if(user!=null){
            user.setState(1);
        }
        session.setAttribute("user",user);
        return "forward:/user/register_ok.jsp";
    }


}
